//
//  NavigationView.m
//  基本框架
//
//  Created by runone02 on 2018/8/3.
//  Copyright © 2018年 liqiyun. All rights reserved.
//

#import "NavigationView.h"

static const CGFloat width = 23; //宽度
static const CGFloat space = 15; //间距

@interface NavigationView ()


@end

@implementation NavigationView

- (instancetype)init{
    self = [super init];
    if (self) {
        self.titleArr = @[@"图片",@"概况",@"信息",@"设施",@"租含",@"安保",@"出行",@"宠物",@"位置",@"退订",@"评价",@"养学",@"周边"];
        [self setupUI];
    }
    return self;
}

- (void)setupUI{
    self.backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.backBtn setImage:IMAGE(@"back_back") forState:UIControlStateNormal];
    self.backBtn.backgroundColor = [UIColor redColor];
    [self.backBtn addTarget:self action:@selector(backViewController) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.backBtn];
    
    [self.backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(10);
        make.top.mas_equalTo(@35);
        make.size.mas_equalTo(CGSizeMake(22, 22));
    }];
    
    self.shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.shareBtn setImage:IMAGE(@"share_back") forState:UIControlStateNormal];
    self.shareBtn.backgroundColor = [UIColor redColor];
    [self.shareBtn addTarget:self action:@selector(shareHouse) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.shareBtn];
    
    [self.shareBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.backBtn);
        make.right.equalTo(self).offset(-73);
        make.size.mas_offset(CGSizeMake(22, 22));
    }];
    
    self.collectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.collectBtn setImage:IMAGE(@"shoucang_back") forState:UIControlStateNormal];
    self.collectBtn.backgroundColor = [UIColor redColor];
    [self.collectBtn setImage:[UIImage imageNamed:@"shoucang_s"] forState:UIControlStateSelected];
    [self.collectBtn addTarget:self action:@selector(collecHouse) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.collectBtn];
    
    [self.collectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.backBtn);
        make.right.equalTo(self).offset(-30);
        make.size.mas_equalTo(CGSizeMake(22, 22));
    }];
    
    self.parting = [[UIView alloc]init];
    self.parting.backgroundColor = [UIColor blackColor];
    [self addSubview:self.parting];
    
    [self.parting mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_right);
        make.centerY.equalTo(self.backBtn);
        make.width.mas_equalTo(0.5);
        make.height.mas_equalTo(14);
    }];
    
    UIView *line = [[UIView alloc]init];
    line.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:line];
    
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self);
        make.right.equalTo(self);
        make.bottom.equalTo(self);
        make.height.mas_equalTo(@0.5);
    }];
    
    self.scrollView = [[UIScrollView alloc]init];
    self.scrollView.showsHorizontalScrollIndicator = NO;
    [self addSubview:self.scrollView];
    
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_right);
        make.bottom.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(100, 50));
    }];
    
    for (int i = 0; i < self.titleArr.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:self.titleArr[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        [btn addTarget:self action:@selector(touchBtn:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = 10000+i;
        btn.titleLabel.font = TITLE_FONT(30);
        [self.scrollView addSubview:btn];
        
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.scrollView).offset(space + (width + space) * i);
            make.centerY.equalTo(self.scrollView).offset(4);
            make.height.mas_equalTo(30);
            make.width.mas_equalTo(50);
        }];
    }
    self.scrollView.contentSize = CGSizeMake(space + (width + space) * self.titleArr.count, 44);
}

- (void)resetBtns{
    for (UIView *btn in self.scrollView.subviews) {
        if ([btn isKindOfClass:[UIButton class]]) {
            UIButton *button = (UIButton*)btn;
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
    }
}

- (void)navigationAnimation{
    if (_animatFinsh == NO) {
        
        for (int i = 0; i<4; i++) {
            switch (i) {
                case 0:{
                    [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionTransitionNone animations:^{
                        [self.shareBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
                            make.centerY.equalTo(self.backBtn);
                            make.left.equalTo(self).offset(39);
                            make.size.mas_offset(CGSizeMake(22, 22));
                        }];
                        [self layoutIfNeeded];
                    } completion:nil];
                    break;
                }
                case 1:{
                    [UIView animateWithDuration:0.5 delay:0.1 options:UIViewAnimationOptionTransitionNone animations:^{
                        [self.collectBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
                            make.centerY.equalTo(self.backBtn);
                            make.left.equalTo(self.shareBtn.mas_right).offset(25);
                            make.size.mas_equalTo(CGSizeMake(22, 22));
                        }];
                        [self layoutIfNeeded];
                    } completion:nil];
                    break;
                }
                case 2:{
                    [UIView animateWithDuration:0.5 delay:0.2 options:UIViewAnimationOptionTransitionNone animations:^{
                        [self.parting mas_remakeConstraints:^(MASConstraintMaker *make) {
                            make.left.equalTo(self.collectBtn.mas_right).offset(25);
                            make.centerY.equalTo(self.backBtn);
                            make.width.mas_equalTo(0.5);
                            make.height.mas_equalTo(14);
                        }];
                        [self layoutIfNeeded];
                    } completion:nil];
                    break;
                }
                case 3:{
                    [UIView animateWithDuration:0.5 delay:0.3 options:UIViewAnimationOptionTransitionNone animations:^{
                        [self.scrollView mas_remakeConstraints:^(MASConstraintMaker *make) {
                            make.left.equalTo(self.parting.mas_right);
                            make.bottom.equalTo(self);
                            make.right.equalTo(self);
                            make.height.mas_equalTo(44);
                        }];
                        [self layoutIfNeeded];
                    } completion:^(BOOL finished) {
                        self.animatFinsh = YES;
                    }];
                    break;
                }
                default:
                    break;
            }
        }
    }
}

- (void)resetFrame{
    if (_animatFinsh == YES) {
        for (int i = 0; i<4; i++) {
            switch (i) {
                case 0:{
                    [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionTransitionNone animations:^{
                        [self.scrollView mas_remakeConstraints:^(MASConstraintMaker *make) {
                            make.left.equalTo(self.mas_right);
                            make.bottom.equalTo(self);
                            make.size.mas_equalTo(CGSizeMake(100, 44));
                        }];
                        [self layoutIfNeeded];
                    } completion:nil];
                    
                    break;
                }
                case 1:{
                    [UIView animateWithDuration:0.3 delay:0.1 options:UIViewAnimationOptionTransitionNone animations:^{
                        [self.parting mas_remakeConstraints:^(MASConstraintMaker *make) {
                            make.left.equalTo(self.mas_right);
                            make.centerY.equalTo(self.backBtn);
                            make.width.mas_equalTo(0.5);
                            make.height.mas_equalTo(14);
                        }];
                        [self layoutIfNeeded];
                    } completion:^(BOOL finished) {
                        
                    }];
                    
                    break;
                }
                case 2:{
                    [UIView animateWithDuration:0.3 delay:0.2 options:UIViewAnimationOptionTransitionNone animations:^{
                        [self.collectBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
                            make.centerY.equalTo(self.backBtn);
                            make.right.equalTo(self).offset(-30);
                            make.size.mas_equalTo(CGSizeMake(22, 22));
                        }];
                        [self layoutIfNeeded];
                    } completion:nil];
                    break;
                }
                case 3:{
                    [UIView animateWithDuration:0.3 delay:0.3 options:UIViewAnimationOptionTransitionNone animations:^{
                        [self.shareBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
                            make.centerY.equalTo(self.backBtn);
                            make.right.equalTo(self).offset(-73);
                            make.size.mas_offset(CGSizeMake(22, 22));
                        }];
                        [self layoutIfNeeded];
                    } completion:^(BOOL finished) {
                       self.animatFinsh = NO;
                    }];
                    break;
                }
                default:
                    break;
            }
        }
    }
}

- (void)touchBtn:(UIButton *)btn{

    NSLog(@"点击了第%ld个按钮",btn.tag);
}

- (void)backViewController{
    [self.viewController.navigationController popViewControllerAnimated:YES];
}

- (void)shareHouse{

    NSLog(@"分享");
}

- (void)collecHouse{

    NSLog(@"收藏");
}
@end
